package top.jianx.music.provider.netease.request;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
//import okhttp3.FormBody;
//import okhttp3.HttpUrl;
//import okhttp3.Request;
//import okhttp3.Response;
import top.jianx.music.base.BaseRequest;
import top.jianx.music.config.Constants;
import top.jianx.music.model.MusicLink;
import top.jianx.music.provider.netease.NeteaseMusicApi;
import top.jianx.music.provider.netease.NeteaseSongLink;
import top.jianx.music.util.CommonUtils;

import java.io.IOException;
import java.util.List;

/**
 * Created by haohua on 2018/2/11.
 */
@SuppressWarnings("SpellCheckingInspection")
public
class NeteaseGetMusicLinksRequest extends BaseRequest<List<? extends MusicLink>> {
    private final String[] mMusicIds;

    public NeteaseGetMusicLinksRequest(String... musicIds) {
        mMusicIds = musicIds;
    }

    @Override
    protected HttpRequest buildRequest() {
        HttpRequest requestBuilder = HttpUtil.createRequest(Method.POST, "http://music.163.com/api/linux/forward");
        requestBuilder.header(Constants.REFERER, "http://music.163.com/");
        requestBuilder.header("X-REAL-IP", CommonUtils.generateChinaRandomIP());

        JSONObject json = new JSONObject();
        json.put("method", "POST");
        json.put("url", "http://music.163.com/api/song/enhance/player/url");
        JSONObject params = new JSONObject();
        JSONArray musicIdArray = new JSONArray();
        for (String musicId : mMusicIds) {
            musicIdArray.add(Long.parseLong(musicId));
        }
        params.put("br", 320000);
        params.put("ids", musicIdArray);
        json.put("params", params);
        String encrypted = NeteaseMusicApi.encrypt(json);
        requestBuilder.form("eparams", encrypted);
        return requestBuilder;



//        Request.Builder requestBuilder = new Request.Builder();
//        requestBuilder.url(HttpUrl.parse("http://music.163.com/api/linux/forward"));
//        requestBuilder.addHeader(Constants.REFERER, "http://music.163.com/");
//        requestBuilder.addHeader("X-REAL-IP", CommonUtils.generateChinaRandomIP());
//        JSONObject json = new JSONObject();
//        json.put("method", "POST");
//        json.put("url", "http://music.163.com/api/song/enhance/player/url");
//        JSONObject params = new JSONObject();
//        JSONArray musicIdArray = new JSONArray();
//        for (String musicId : mMusicIds) {
//            musicIdArray.add(Long.parseLong(musicId));
//        }
//        params.put("br", 320000);
//        params.put("ids", musicIdArray);
//        json.put("params", params);
//        String encrypted = NeteaseMusicApi.encrypt(json);
//        FormBody body = new FormBody.Builder().add("eparams", encrypted).build();
//        requestBuilder.post(body);
//        final Request request = requestBuilder.build();
//        return request;
    }

    @Override
    protected List<NeteaseSongLink> parseResult(HttpResponse response) throws IOException {
        JSONObject responseJson = JSONObject.parseObject(response.body());
        JSONArray jsonData = responseJson.getJSONArray("data");
        List<NeteaseSongLink> songLinks = jsonData.toJavaList(NeteaseSongLink.class);
        return songLinks;
    }
}
